from bilibliwebutils import *
import asyncio
import aiohttp
import time

cor_data_path = "./data_cor/"
async def routine_task(url:str):
    """
    一个协程任务，请求html，解析后写入本地
    :param url:
    :return:
    """
    # print("《《" * 10 + url + "》》》" * 10)
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as resp:
            html_str = await resp.text()
            items = parse_rank_html(html_str, True)
            file_path = cor_data_path + url.split("/")[-1] + ".txt"
            write_local_data(items, file_path)

def corountine():
    """
    创建协程循环，启动携程任务
    :return:
    """
    loop = asyncio.get_event_loop()
    tasks = [loop.create_task(routine_task(B_RANK_URL + api)) for api in B_RANK_APIS]
    loop.run_until_complete(asyncio.wait(tasks))
    loop.close()

if __name__ == "__main__":
    create_floder(cor_data_path)
    start = time.time()
    corountine()
    end = time.time()
    print("\n 》》》corountine thread cost:" + str(end - start) + " seconds")

